﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Threading;
using System.Web;

namespace Aide4
{
    public class IpHelper
    {
        /// <summary>
        /// 获取内网IP
        /// </summary>
        /// <returns></returns>
        public static IPAddress GetInternalIP()
        {
            NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();

            foreach (NetworkInterface adapter in nics)
            {
                foreach (var uni in adapter.GetIPProperties().UnicastAddresses)
                {
                    if (uni.Address.AddressFamily == AddressFamily.InterNetwork)
                    {
                        return uni.Address;
                    }
                }
            }
            return null;
        }

        /// <summary>
        ///获取宽带连接(PPPOE拨号)的IP地址，当宽带未连接或者连接中的时候获取不到IP
        /// </summary>
        /// <returns>IP地址</returns>
        public static string GetInterNetIP()
        {
            int i = 3;//超时秒

            while (i > 0)
            {
                try
                {
                    NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
                    bool havePPPOE = false;
                    foreach (NetworkInterface adapter in nics)
                    {
                        if (adapter.NetworkInterfaceType == NetworkInterfaceType.Ppp)
                        {
                            havePPPOE = true;
                            IPInterfaceProperties ip = adapter.GetIPProperties(); //IP配置信息

                            //获取单播地址集
                            UnicastIPAddressInformationCollection ipCollection = ip.UnicastAddresses;
                            foreach (UnicastIPAddressInformation ipadd in ipCollection)
                            {
                                //InterNetwork    IPV4地址      InterNetworkV6        IPV6地址
                                //Max            MAX 位址
                                if (ipadd.Address.AddressFamily == AddressFamily.InterNetwork)
                                    //判断是否为ipv4
                                    return ipadd.Address.ToString();//获取ip
                            }
                        }
                    }
                    //当没有宽带连接的时候直接返回空
                    if (!havePPPOE) return "";
                }
                catch (Exception ex)
                {
                    Console.WriteLine("获取宽带拨号IP出错:" + ex.Message);
                }
                i--;
                Thread.Sleep(500);
            }
            return "";

        }

        /// <summary>
        ///获取宽带连接(PPPOE拨号)的IP地址，当宽带未连接或者连接中的时候获取不到IP
        /// </summary>
        /// <returns>IP地址</returns>
        public static string GetInterNetIPV6()
        {
            int i = 3;//超时秒

            while (i > 0)
            {
                try
                {
                    NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
                    bool havePPPOE = false;
                    foreach (NetworkInterface adapter in nics)
                    {
                        if (adapter.NetworkInterfaceType == NetworkInterfaceType.Ppp)
                        {
                            havePPPOE = true;
                            IPInterfaceProperties ip = adapter.GetIPProperties(); //IP配置信息
                            if (ip.UnicastAddresses.Count > 0)
                            {
                                return ip.UnicastAddresses[0].Address.ToString();
                            }
                        }
                    }
                    //当没有宽带连接的时候直接返回空
                    if (!havePPPOE) return "";
                }
                catch (Exception ex)
                {
                    Console.WriteLine("获取宽带拨号IP出错:" + ex.Message);
                }
                i--;
                Thread.Sleep(500);
            }
            return "";

        }

    }
}